package org.skydiveapphd;

import android.app.AlertDialog;
import android.app.Fragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import org.skydiveapphd.interfaces.NotesNotifier;
import org.skydiveapphd.ui.FragmentHelper;
import org.skydiveapplibrary.R;
import org.skydiveapplibrary.data.Location;
import org.skydiveapplibrary.repository.LocationRepository;

public class LocationEditActivity extends Fragment implements NotesNotifier
{
	// repository
	private LocationRepository repository;
	
	// data
	private boolean isEdit = false;
	private Location location;
	
	// listeners
	private OnClickListener saveListener = new OnClickListener()
	{
		@Override
		public void onClick(View view)
		{
			save();
		}
	};
	private OnClickListener cancelListener = new OnClickListener()
	{
		@Override
		public void onClick(View view)
		{
			getFragmentManager().popBackStack();
		}
	};
	private OnClickListener notesEditListener = new OnClickListener()
	{
		@Override
		public void onClick(View view)
		{
			commitChanges();
			
			Bundle args = new Bundle();
			args.putString(Constants.EXTRA_FRAG_TAG, LocationEditActivity.this.getTag());
			args.putString(Constants.EXTRA_NOTES, location.getNotes());
			
			FragmentHelper.getInstance().openFragment(NotesEditActivity.newInstance(args), android.R.id.tabhost, 
					Constants.TAG_NOTES_EDIT, getFragmentManager());
		}
	};
	private OnClickListener deleteListener = new OnClickListener()
	{
		@Override
		public void onClick(View view)
		{
			showDeleteWarning();
		}
	};
	private DialogInterface.OnClickListener deleteConfirmListener = new DialogInterface.OnClickListener()
	{
		@Override
		public void onClick(DialogInterface arg0, int arg1)
		{
			delete();
		}
	};
	
	public static LocationEditActivity newInstance(Bundle args) 
	{
		LocationEditActivity f = new LocationEditActivity();
		f.setArguments(args);
		return f;
	}
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		// load data
		repository = new LocationRepository(getActivity());
		loadData();
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
	    		Bundle savedInstanceState)
	{	
	    // Inflate the layout for this fragment
	    return inflater.inflate(R.layout.location_edit_view, container, false);
	}
	
	@Override
	public void onResume()
	{
		super.onResume();
		
		initializeUI();
	}
	
	private void loadData()
	{
		// set isEdit flag
		isEdit = getArguments().containsKey(Constants.EXTRA_LOCATION_ID);
		
		// determine add vs. edit
		if (isEdit)
		{
			// editing
			int locationId = getArguments().getInt(Constants.EXTRA_LOCATION_ID);
			location = repository.getLocation(locationId);
		}
		else
		{
			// adding
			location = new Location();
		}
	}
	
	private void initializeUI()
	{	
		// set header text
		TextView headerTextView = (TextView) getActivity().findViewById(R.id.EditHeaderTextView);
		headerTextView.setText(isEdit ?
				R.string.location_edit_label : R.string.location_new_label);
		
		// set name
		EditText nField = (EditText) getActivity().findViewById(R.id.LocationNameField);
		nField.setText(location.getName());
		
		// set home
		CheckBox hField = (CheckBox) getActivity().findViewById(R.id.LocationHomeField);
		hField.setChecked(location.isHome());
		
		// set notes
		TextView notesField = (TextView) getActivity().findViewById(R.id.LocationNotesField);
		notesField.setText(location.getNotes());
		notesField.setOnClickListener(notesEditListener);
		
		// add button listeners
		ImageButton saveButton = (ImageButton) getActivity().findViewById(R.id.LocationEditSaveButton);
		saveButton.setOnClickListener(saveListener);
		
		ImageButton cancelButton = (ImageButton) getActivity().findViewById(R.id.LocationEditCancelButton);
		cancelButton.setOnClickListener(cancelListener);
		
		ImageButton deleteButton = (ImageButton) getActivity().findViewById(R.id.LocationEditDeleteButton);
		deleteButton.setOnClickListener(deleteListener);
		
		// show/hide delete button
		deleteButton.setVisibility(isEdit ? View.VISIBLE : View.GONE);
	}
	
	private void showDeleteWarning()
	{
		// create alert dialog
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		builder.setMessage(R.string.location_delete_warning);
		builder.setIcon(android.R.drawable.ic_dialog_alert);
		builder.setTitle(R.string.delete_confirmation);
		builder.setPositiveButton(R.string.yes_button, deleteConfirmListener);
		builder.setNegativeButton(R.string.no_button, null);
		AlertDialog dialog = builder.create();
		// show dialog
		dialog.show();
	}
	
	public void updateNotes(String notes)
	{
		// update location
		location.setNotes(notes);
	}
	
	private void commitChanges()
	{
		// set name
		EditText nField = (EditText) getActivity().findViewById(R.id.LocationNameField);
		String name = nField.getText().toString();
		location.setName(name);
		
		// set is home
		CheckBox hField = (CheckBox) getActivity().findViewById(R.id.LocationHomeField);
		location.setHome(hField.isChecked());  
	}

	private void save()
	{
		commitChanges();
				
		// save
		if (isEdit)
		{
			repository.updateLocation(location);
		}
		else
		{
			repository.addLocation(location);
		}
		
		// finish
		getFragmentManager().popBackStack();
	}
	
	private void delete()
	{
		// delete location
		repository.deleteLocation(location);
		
		// finish
		getFragmentManager().popBackStack();
	}
}